package cn.tom.service.impl;

import cn.tom.dao.TypeDao;
import cn.tom.entity.Type;
import cn.tom.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;

@Service
@CacheConfig(cacheNames = "type")
/**
 *  注解式缓存 的 方式 是以 String 的方式 存储的
 * */
public class TypeServiceImpl implements TypeService {
    @Resource
    private TypeDao typeDao;

    @Override
    @CachePut(value = "type" , key = "#type")
    public Type update(Type type) {
        typeDao.update(type);
        return typeDao.findById(type.getId());
    }

    @Override
    @CachePut(value = "type" , key = "#type")
    public int add(Type type) {
        return typeDao.add(type);
    }

    @Override
    @Cacheable(value = "type" , key = "#id")
    public Type findById(Integer id) {
        return typeDao.findById(id);
    }

    @Override
    @CacheEvict(value = "type" , key = "#id")
    public int remove(Integer id) {
        return typeDao.remove(id);
    }

    @Override
    @Cacheable(value = "type")
    public List<Type> findAll() {
        return typeDao.findAll();
    }
}
